/*
 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "board.h"
#include <stdio.h>
#include <stdlib.h>
#include "asr_rv32.h"
#include "asr_pinmux.h"
#include "asr_uart.h"
#include "printf_uart.h"
#if 0
#include "los_config.h"
#include "los_debug.h"
#include "los_interrupt.h"
#include "los_task.h"
#include "los_tick.h"
#else
#include "cmsis_os.h"
#include "lega_at_api.h"
#endif
#include "los_exc_info.h"
#include "alto_version.h"
/*
main task stask size(byte)
*/
#define OS_MAIN_TASK_STACK (4096)

osThreadAttr_t g_main_task={"main_task",0,NULL,0,NULL,OS_MAIN_TASK_STACK,15,0,0};

extern void HalPendSV(void);
void PendSV_Handler(void)
{
    HalPendSV();
}
#ifdef MQTTV5_DEMO
extern void mqttv5_demo_init(void);
#endif
static void sys_init(void)
{
    printf("sys_init running1...\n");

	char dtm[100];
	int year, month, day;

#ifdef RELEASE_DOMAIN
    //LOG("log_disable");
    lega_log_disable();
#endif
    alto_flash_kv_init();
//	int a = 0;
//	alto_flash_kv_set("KV_FILE_SUM",(void *)&a,1,1);

//	UtilsSetValue("KV_FILE_SUM",0);

    board_after_init();
    //debug_memory_access_err_check(0x0, 0x10000, MPU_AP_NA_NA);

#ifdef SYSTEM_COREDUMP
    //coredump_command_register(0, NULL);
#endif

#ifdef CFG_DUAL_AP
    comm_wifi_command_register(0,NULL);
#endif

    lega_at_init(AT_TASK_NAME,AT_TASK_PRIORITY,AT_TASK_STACK_SIZE);
    lega_at_cmd_register_all();

#ifdef ASR_PROFILE_ON
    asr_profile_config();
#endif

#ifdef LWIP_APP_IPERF
    lega_wifi_iperf_init();
#endif
#ifdef MQTTV5_DEMO
    mqttv5_demo_init();
#endif
#ifdef BLE_APP_AT_CMD
    extern void lega_ble_at_init(void);
    lega_ble_at_init();
#endif

#ifdef CFG_MRFOTA_TEST
    //extern void lega_rfota_wifi_test_at_init(void);
    //lega_rfota_wifi_test_at_init();
    //extern void lega_rfota_ble_test_at_init(void);
    //lega_rfota_ble_test_at_init();
#endif
#ifndef CFG_HARMONY_SUPPORT

    extern void  OHOS_SystemInit(void);
    OHOS_SystemInit();
#endif
    printf("sys_init running2...\n");
}

#include "systick_delay.h"

void main_test(void)
{
	while(1)
	{
//		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
//		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
		//delay_ms(500);
		osDelay (10*1000);
		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
//		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
//		PRINTK("%s L%d\n",__FUNCTION__,__LINE__);
	}
}
int main(void)
{
    osStatus_t ret;
    osThreadId_t threadId;
#if	1
//    UINT32 uwRet;
//    UINT32 taskID;
//    TSK_INIT_PARAM_S stTask = {0};
//    (VOID)memset_s((VOID *)(&stTask), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
//
//    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)main_test;
//    stTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
//    stTask.pcName = "main_test";
//    stTask.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO; /* Os task priority is 6 */
#endif
    board_before_init();
    //board_uart_init();

    board_init();
    board_uart_init();
    printf("\r\n version app:%s\r\n", LEGA_VERSION_STR);
#if LOSCFG_PLATFORM_EXC == 1
    OsExcMsgDumpInit();
#endif

#ifdef DEBUG_BACKTRACE
    debug_backtrace_init();
#endif // DEBUG_BACKTRACE
    ret = osKernelInitialize();

    if(ret == osOK)
    {
#if 1    
//	    uwRet = LOS_TaskCreate(&taskID, &stTask);
//	    if (uwRet != LOS_OK) {
//	        printf("Task1 create failed\n");
//	    }else{
//			PRINTK("%s L%d taskID:%d\n",__FUNCTION__,__LINE__,taskID);
//
//		}
        threadId = osThreadNew((osThreadFunc_t)sys_init,NULL,&g_main_task);
        if(threadId!=NULL)
        {

            osKernelStart();
        }
#endif
    }   

    while(1);    
    return 0;
}
